#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <stdio.h>
#include <sys/time.h>
#include <opencv/highgui.h>

#include "colors.h"

using namespace cv;
using namespace std;


class RectangleScanner {
 
    public:

    RectangleScanner();

    //Getters and setters

    void SetImage(Mat image);

    int GetHeight();
    int GetWidth();
    RotatedRect GetRectangle();

    //The functional parts
    void PrepareImage();
    void FindContours();
    void FindRectangles();
    void ChooseRectangle();
    
    typedef enum {TOP = 3, MIDDLE = 2, OTHER = 1} RECT;

    RECT best_rect_type;
    RotatedRect best_match_rect;
    bool top_rect_selected;
    bool middle_rect_selected;
    
    Mat src_copy, src;
#if defined(SAVE_IMAGES) || defined(WINDOW)
    Mat src_rects;
#endif /* SAVE_IMAGES */
    vector<RotatedRect> rectangles;

    private:

    int getArea(vector<Point>);

    //Required objects for image processing
    Mat gray;

    //Places to store 
    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours2;
    vector<vector<Point> > contours;


};


void saveFrame(char *fileBase, Mat *frame);

void processFrame(Mat &, RectangleScanner &);
